package com.czl.module_service.activity

import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.Html
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.czl.base.base.BaseActivity
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.NoticeListBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.R
import com.czl.module_service.BR
import com.czl.module_service.databinding.ActivityNoticeDetailBinding
import com.czl.module_service.viewmodel.NoticeDetailViewModel
import org.xml.sax.XMLReader
import java.net.URLDecoder
import java.util.*
import com.czl.module_service.util.UrlImageGetter
import anet.channel.util.Utils.context

@Route(path = AppConstants.Router.Service.F_NOTICE_DETAIL)
class NoticeDetailActivity : BaseActivity<ActivityNoticeDetailBinding, NoticeDetailViewModel>() {

    private var imgGetter: UrlImageGetter? = null

    private var bean: NoticeListBean.Data? = null

    override fun initData() {
        viewModel.tvTitle.set("疫情公告详情")
        bean = intent.getSerializableExtra("data") as NoticeListBean.Data
        viewModel.selectAnnouncementById(bean!!.announcementId)
        viewModel.updaReadingVolumeById(bean!!.announcementId)
        val viewBinding: ActivityNoticeDetailBinding = binding
        imgGetter = UrlImageGetter(applicationContext, viewBinding.richTextExp)
    }

    override fun initContentView(): Int {
        return R.layout.activity_notice_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            // 前一页跳转对象数据
            val viewBinding: ActivityNoticeDetailBinding = binding
            viewBinding.tvTitle.setText(data.announcementTitle)
            viewBinding.tvCreateTime.setText(data.publisher + " 发布于 " + TimeUtils.date2String(
                Date(data.releaseTime),
                "yyyy-MM-dd HH:mm"
            ))
            viewBinding.tvReadNum.setText(data.readingVolume.toString())
            val str = URLDecoder.decode(data.announcementContent, "UTF-8")
//            viewBinding.richTextExp.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY))
            viewBinding.richTextExp.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY, imgGetter, null));

        })
    }
}